(define-module (guix import tests crate-git)
  #:use-module (guix import crate-git)
  #:use-module (guix import utils)
  #:use-module (ice-9 threads)
  #:use-module (semver)
  #:use-module (json)
  #:use-module ((srfi srfi-26) #:select (cut)))

(define all-crate-names
  (with-input-from-file (string-append %crates.io-index "/all-crate-names")
    read-lines))
(display "All crate names read: ") (display (car all-crate-names)) (display "...") (newline)
;;  this means we can parse all the json
(define all-crate-data (map lookup-crate-data all-crate-names))
(display "All crate data read: ") (display (car all-crate-data)) (display "...") (newline)

(define all-versions
  (let ((get-vers (cut assoc-ref <> "vers")))
    (apply append
           (map (cut map get-vers <>)
                all-crate-data))))
(display "All versions data read: ") (display (car all-versions)) (display "...") (newline)

(define all-semvers (map string->semver all-versions))
(display "All semvers converted: ") (display (car all-semvers)) (display "...") (newline)

(define all-lines
  (let ((read-lines-from-file
         (lambda (file)
           (with-input-from-file file read-lines))))
    (apply append
           (n-par-map (current-processor-count)
                      read-lines-from-file
                      (map crate-data-path all-crate-names)))))
(define all-lines2
  (let ((read-lines-from-file
         (lambda (file)
           (with-input-from-file file read))))
    (n-par-map (current-processor-count)
               read-lines-from-file
               (map crate-data-path all-crate-names))))
(define all-json-lines
  ;; Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS
  (map json-string->scm all-lines))
